package com.lovo.dao.impl;

import com.lovo.bean.CutPageBean;
import com.lovo.bean.DeptBean;
import com.lovo.dao.BaseDao;
import com.lovo.dao.IDeptDao;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class DeptDaoImpl extends BaseDao implements IDeptDao {
    @Override
    public List<DeptBean> findAll() {
        List list = new ArrayList();
        String sql = "select * from dept";

        try{
            setConnection();
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){

                list.add(fillDept());
            }

        }catch(Exception e){
            e.printStackTrace();
        }finally {
            closeConnection();
        }
        return list;

    }

    @Override
    public CutPageBean<DeptBean> findByItem(int pageNo) {
        CutPageBean<DeptBean> cutPageBean = new CutPageBean<>();
        String sql = "select * from dept  limit "+(pageNo - 1) * CutPageBean.PAGESIZE+","+CutPageBean.PAGESIZE;

        try{
            setConnection();
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){

                cutPageBean.getList().add(fillDept());
            }
            ps = con.prepareStatement("select count(*) cnt from dept");
            rs = ps.executeQuery();
            rs.next();
            cutPageBean.setCount(rs.getInt("cnt"));

        }catch(Exception e){
            e.printStackTrace();
        }finally {
            closeConnection();
        }

        return cutPageBean;
    }

    @Override
    public void add(DeptBean deptBean) {
        try{
            this.setConnection();
            ps = con.prepareStatement("insert into dept(dept_name,create_time,info) values(?,?,?)");
            ps.setObject(1,deptBean.getName());
            ps.setObject(2,deptBean.getCreateTime());
            ps.setObject(3,deptBean.getInfo());
            ps.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public void update(DeptBean deptBean) {
        try {
            this.setConnection();
            ps = con.prepareStatement("update dept set dept_name=?,create_time=?,info=? where dept_id=?");
            ps.setObject(1, deptBean.getName());
            ps.setObject(2, deptBean.getCreateTime());
            ps.setObject(3, deptBean.getInfo());
            ps.setObject(4, deptBean.getId());
            ps.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    private DeptBean fillDept() throws SQLException {
        DeptBean deptBean = new DeptBean();
        deptBean.setId(rs.getInt("dept_id"));
        deptBean.setName(rs.getString("dept_name"));
        deptBean.setInfo(rs.getString("info"));
        if(rs.getString("create_time") != null){
            deptBean.setCreateTime(LocalDate.parse(rs.getString("create_time")));
        }
        return deptBean;
    }

    public static void main(String[] args) {
        DeptDaoImpl deptDao = new DeptDaoImpl();
        System.out.println(deptDao.findByItem(1));

//        DeptBean deptBean = new DeptBean(1,"市场部",null,"xxx");

//        deptDao.add(deptBean);
//        deptDao.update(deptBean);
    }
}
